// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   MakePdf.java

package include.nseer_cookie;

import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import include.excel_export.Masking;
import include.nseer_db.nseer_db;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

public class MakePdf
{

	private int fileAmount;
	private int allpage;
	private String configFile;

	public MakePdf()
	{
		fileAmount = 0;
		allpage = 0;
	}

	public void setConfigFile(String configFile)
	{
		this.configFile = configFile;
	}

	public void make(String database, String tablename, String sql1, String sql2, String filename, int everypage, HttpSession session)
	{
		try
		{
			nseer_db aaa = new nseer_db(database);
			nseer_db demo_db = new nseer_db(database);
			ServletContext context = session.getServletContext();
			String path = context.getRealPath("/");
			Masking reader = new Masking(configFile);
			Vector columnNames = new Vector();
			Vector tables = reader.getTableNicks();
			for (Iterator loop = tables.iterator(); loop.hasNext();)
			{
				String tablenick = (String)loop.next();
				columnNames = reader.getColumnNames(tablenick);
			}

			int cpage = 1;
			int spage = 1;
			int ipage = everypage;
			String pagesql = sql1;
			ResultSet pagers = demo_db.executeQuery(pagesql);
			pagers.next();
			int allCol = pagers.getInt("A");
			allpage = (int)Math.ceil(((allCol + ipage) - 1) / ipage);
			for (int m = 1; m <= allpage; m++)
			{
				spage = (m - 1) * ipage;
				String sql = (new StringBuilder(String.valueOf(sql2))).append(" limit ").append(spage).append(",").append(ipage).toString();
				ResultSet bbb = aaa.executeQuery(sql);
				int b = columnNames.size();
				int a;
				for (a = 0; bbb.next(); a++);
				bbb.first();
				Rectangle rectPageSize = new Rectangle(PageSize.A4);
				rectPageSize = rectPageSize.rotate();
				Document document = new Document(rectPageSize, 20F, 20F, 20F, 20F);
				PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream((new StringBuilder(String.valueOf(path))).append(filename).append(m).append(".pdf").toString()));
				document.open();
				BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
				Font FontChinese = new Font(bfChinese, 8F, 0);
				Paragraph title1 = new Paragraph("nseer ERP", FontFactory.getFont("Helvetica", 18F, 3));
				Chapter chapter1 = new Chapter(title1, 1);
				chapter1.setNumberDepth(0);
				Paragraph title11 = new Paragraph(tablename, FontFactory.getFont("Helvetica", 16F, 1));
				Section section1 = chapter1.addSection(title11);
				Table t = new Table(b, a);
				t.setPadding(1.0F);
				t.setSpacing(0.0F);
				t.setBorderWidth(1.0F);
				do
				{
					for (int k = 0; k < b; k++)
					{
						Cell cell = new Cell(new Paragraph(bbb.getString((String)columnNames.elementAt(k)), FontChinese));
						t.addCell(cell);
					}

				} while (bbb.next());
				section1.add(t);
				document.add(chapter1);
				document.close();
			}

		}
		catch (Exception pp)
		{
			pp.printStackTrace();
		}
	}

	public int fileAmount()
	{
		fileAmount = allpage;
		return fileAmount;
	}
}
